﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using HotelSoft.HotelManagement_BUS;
using HotelSoft.HotelManagement_DTO;
using System.Collections;

namespace HotelSoft.HotelManagement_GUI
{
    public partial class frmQuanly_KHACHHANG : Form
    {
        private bool flag = true;
        private static string chungminhnhandan;
        private static int dem=0;

        public string Chungminhnhandan
        {
            get { return chungminhnhandan; }
            set { chungminhnhandan = value; }
        }

        Double temp = 1;
        KhachHangBUS busKhachHang;
        public KhachHangDTO dtoKhachHang;
        ArrayList dsKhachHang;
        BindingSource bsData = null;
        public int iCo = 0;
        public bool bNhapDung = false;
        public bool kt = false;
        private static string maKH; // nhan du lieu MAKH tu form Tra cuu

        // Define the CS_DROPSHADOW constant
        private const int CS_DROPSHADOW = 0x00020000;

        // Override the CreateParams property
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }
        public string MAKHACHHANG
        {
            get { return maKH; }
            set { maKH = value; }
        }

        public frmQuanly_KHACHHANG()
        {
            InitializeComponent();
        }

        private void frmQuanly_KHACHHANG_Load(object sender, EventArgs e)
        {
            CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
            Thread.CurrentThread.CurrentCulture = culture;
            timer1.Enabled = true;
            LayDanhSachKhachHang();
            Ra_Nam.Checked = true;

            

            if (Grid_KhachHang.SelectedRows.Count == 0)
            {
                DieukienMo(true);
                Txt_CMND.Focus();
                kt = true;

            }
            DieukienMo(false);
            Dt_NgaySinh.Text = DateTime.Now.ToShortDateString().ToString();

            if (maKH != null)
                MessageBox.Show(maKH.ToString());
        }

        public void LamMoi()
        {
            Txt_CMND.Text = "";
            Txt_HoTen.Text = "";
            Dt_NgaySinh.IsEmpty = true;
            Txt_QuocTich.Text = "";
            Txt_DiaChi.Text = "";
            Txt_Sodt.Text = "";
            Ra_Nam.Checked = true;

        }
        public void DieukienMo(bool Mo)
        {
            Txt_CMND.Enabled = Mo;
            Txt_HoTen.Enabled = Mo;
            Txt_DiaChi.Enabled = Mo;
            Txt_QuocTich.Enabled = Mo;
            Txt_Sodt.Enabled = Mo;
            Dt_NgaySinh.Enabled = Mo;
            groupBox1.Enabled = Mo;
            Gru_LKL.Enabled = Mo;
            Gru_TXS.Enabled = !Mo;
        }
        private void LayDanhSachKhachHang()
        {
            busKhachHang = new KhachHangBUS();
            dsKhachHang = busKhachHang.LayDanhSachKhachHang();
            XuatDanhSachKhachHang(dsKhachHang);
            Grid_KhachHang.CellContentClick += new DataGridViewCellEventHandler(Grid_KhachHang_CellContentClick);

        }
        private void XuatDanhSachKhachHang(ArrayList dsKhachHang)
        {
            Grid_KhachHang.AutoGenerateColumns = false;

            this.bsData = new BindingSource(dsKhachHang, "");
            Grid_KhachHang.DataSource = bsData;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Double temp = 1;
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
            {
                timer1.Enabled = false;
            }
        }

        private void Grid_KhachHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Grid_KhachHang.SelectedCells.Count > 0)
            {
                int row = Grid_KhachHang.SelectedCells[0].RowIndex;
                Txt_CMND.Text = Grid_KhachHang.Rows[row].Cells[1].Value.ToString();
                Txt_HoTen.Text = Grid_KhachHang.Rows[row].Cells[2].Value.ToString();
                if (Grid_KhachHang.Rows[row].Cells[3].Value.ToString() == "Nam")
                    Ra_Nam.Checked = true;
                else if (Grid_KhachHang.Rows[row].Cells[3].Value.ToString() == "Nữ")
                    Ra_Nu.Checked = true;
                Dt_NgaySinh.Text = Grid_KhachHang.Rows[row].Cells[4].Value.ToString();
                Txt_DiaChi.Text = Grid_KhachHang.Rows[row].Cells[5].Value.ToString();
                Txt_Sodt.Text = Grid_KhachHang.Rows[row].Cells[6].Value.ToString();
                Txt_QuocTich.Text = Grid_KhachHang.Rows[row].Cells[7].Value.ToString();
            }
        }

        private bool KiemTraPhongCoChua()
        {
            KhachHangDTO dto = new KhachHangDTO();
            dto.CMND = Txt_CMND.Text;
            return busKhachHang.KTMA(dto);
        }
        private bool KiemTraDuLieuNhap()
        {
            if (Txt_HoTen.Text.Length == 0 || Txt_HoTen.Text.Trim() == "" || 
                Txt_DiaChi.Text.Length == 0 || Txt_DiaChi.Text.Trim() == "" ||
                Txt_QuocTich.Text.Length == 0 || Txt_QuocTich.Text.Trim() == "" ||
                Txt_Sodt.Text.Length == 0 || Txt_Sodt.Text.Trim() == "" ||
                Txt_CMND.Text.Length == 0 || Txt_CMND.Text.Trim() == "")
            {
                MessageBox.Show("Xin hãy kiểm tra lại dữ liệu nhập.\nXin hãy nhập đủ thông tin!");
                return false;
            }
            return true;
        }
        
        private int LayKhachHangDuocChon(out ArrayList dsKhachHang)
        {
            int iIndex = 0;
            dsKhachHang = new ArrayList();
            foreach (DataGridViewRow dong in this.Grid_KhachHang.Rows)
            {
                DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dong.Cells[0];
                bool bChecked = (null != cell && null != cell.Value && true == (bool)cell.Value);
                if (bChecked)
                {
                    dsKhachHang.Add(dong.DataBoundItem);
                    iIndex = dong.Index;
                }
            }

            return iIndex;
        }
        private void XoaNhanVienDuocChonKhoiDanhSach()
        {
            for (int i = 0; i < Grid_KhachHang.Rows.Count; i++)
            {
                DataGridViewRow dong = Grid_KhachHang.Rows[i];
                DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dong.Cells[0];
                KhachHangDTO dtoPhongBiXoa = (KhachHangDTO)Grid_KhachHang.Rows[cell.RowIndex].DataBoundItem;

                bool bChecked = (null != cell && null != cell.Value && true == (bool)cell.Value);
                if (bChecked)
                {
                    this.bsData.RemoveAt(cell.RowIndex);
                    i--;
                }
            }
        }
        private void XoaKhachHang()
        {
            ArrayList dsKhachHang;
            LayKhachHangDuocChon(out dsKhachHang);
            if (dsKhachHang.Count > 0)
            {
                KhachHangBUS busPhong = new KhachHangBUS();
                busKhachHang.Xoa(dsKhachHang);
                XoaNhanVienDuocChonKhoiDanhSach();
            }
            else
            {
                MessageBox.Show("Xin hãy chọn một Khách Hàng muốn xóa!");
                return;
            }
        }

        private void bt_Them_Click(object sender, EventArgs e)
        {
            DieukienMo(true);
            //Txt_CMND.ReadOnly = true;
            ////Txt_CMND.Focus();
            //Txt_CMND.Text = chungminhnhandan;
            //Txt_HoTen.Enabled = true;
            //Txt_HoTen.Focus();
            //Txt_CMND.Focus();
            LamMoi();

            if (flag == true && chungminhnhandan!=null)
            {
                Txt_HoTen.Focus();
                Txt_CMND.ReadOnly = true;
                Txt_CMND.Text = chungminhnhandan;
                flag = false;
            }
            else
            {
                Txt_CMND.ReadOnly = false;
                Txt_CMND.Focus();
            }
            kt = true;
        }
        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (Txt_CMND.Text!="" && Txt_HoTen.Text!="")
            {
                DieukienMo(true);
                Txt_CMND.Enabled = false;
                kt = false;
            }
            else
            {
                MessageBox.Show("Xin hãy chọn một Khách Hàng muốn sửa thông tin!");
            }
        }
        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            ArrayList dsKhachHang;
            LayKhachHangDuocChon(out dsKhachHang);
            if (dsKhachHang.Count > 0)
            {
                DialogResult kq = MessageBox.Show("Bạn có thật sự muốn xóa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (kq == DialogResult.Yes)
                {
                    XoaKhachHang();
                    iCo = 1;
                    LamMoi();
                }
            }
            else
            {
                MessageBox.Show("Xin hãy chọn một Khách Hàng muốn xóa!");
            }
        }
        
        private void Btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void Btn_TraCuu_Click(object sender, EventArgs e)
        {
            KhachHangDTO dtoKhaChHangCanTraCuu = new KhachHangDTO();

            if (Txt_CMND.Enabled == false && Txt_HoTen.Enabled == false)
            {
                MessageBox.Show("Xin hãy nhấn nút mở ô nhập liệu\nđể tiến hành nhập liệu và tra cứu", "Thông báo tra cứu");
                Btn_LamLai.Focus();
                return;
            }
            if (Txt_HoTen.Text == "")
                dtoKhaChHangCanTraCuu.HOTENKH = null;
            else
                dtoKhaChHangCanTraCuu.HOTENKH = Txt_HoTen.Text.ToString();
            if (Dt_NgaySinh.Value == null)
                dtoKhaChHangCanTraCuu.NGAYSINH = null;
            else
                dtoKhaChHangCanTraCuu.NGAYSINH = Dt_NgaySinh.Text.ToString();
            if (Txt_CMND.Text == "")
                dtoKhaChHangCanTraCuu.CMND = null;
            else
                dtoKhaChHangCanTraCuu.CMND = Txt_CMND.Text.ToString();

            dtoKhaChHangCanTraCuu.DIACHI = null;
            dtoKhaChHangCanTraCuu.SDTKH = null;
            dtoKhaChHangCanTraCuu.QUOCTICH = null;
            dtoKhaChHangCanTraCuu.GIOITINH = null;
                    
            ArrayList dsTraCuuDuoc = busKhachHang.TraCuuKhachHang(dsKhachHang, dtoKhaChHangCanTraCuu);
            XuatDanhSachKhachHang(dsTraCuuDuoc);
            LamMoi();
            DieukienMo(false);
            if (Grid_KhachHang.RowCount == 0)
            {
                MessageBox.Show("Không tìm thấy thông tin do bạn nhập\nXin vui lòng nhập lại thông tin khác", "Thông báo tra cứu");
            }
        }

        private void Btn_LamLai_Click(object sender, EventArgs e)
        {
            LamMoi();
            DieukienMo(false);
            LayDanhSachKhachHang();
        }

        private void bt_Luu_Click(object sender, EventArgs e)
        {
            busKhachHang = new KhachHangBUS();
            try
            {
                if (kt == true)
                {
                    dtoKhachHang = new KhachHangDTO();
                    bNhapDung = KiemTraDuLieuNhap();
                    if (bNhapDung)
                    {
                        if (KiemTraPhongCoChua())
                        {
                            MessageBox.Show("CMND/PASSPORT này đã tồn tại, xin nhập lại!");
                            Txt_CMND.Focus();
                            Txt_CMND.SelectAll();
                            return;
                        }

                        dtoKhachHang.CMND = Txt_CMND.Text.ToString();
                        dtoKhachHang.HOTENKH = Txt_HoTen.Text.ToString();
                        if (Ra_Nam.Checked == true)
                            dtoKhachHang.GIOITINH = Ra_Nam.Text.ToString();
                        else

                            dtoKhachHang.GIOITINH = Ra_Nu.Text.ToString();

                        dtoKhachHang.NGAYSINH = Dt_NgaySinh.Text.ToString();
                        dtoKhachHang.DIACHI = Txt_DiaChi.Text.ToString();
                        dtoKhachHang.QUOCTICH = Txt_QuocTich.Text.ToString();
                        dtoKhachHang.SDTKH = Txt_Sodt.Text.ToString();
                    
                        if(!busKhachHang.Them(dtoKhachHang))
                        {
                            MessageBox.Show("Thất bại! Xin hãy kiểm tra lại");
                            return;
                        }

                        if (bsData.Count == 0)
                        {
                            ArrayList dsKhachHang = new ArrayList();
                            dsKhachHang.Add(dtoKhachHang);
                            bsData = new BindingSource(dsKhachHang, "");
                            this.Grid_KhachHang.DataSource = bsData;
                        }
                        else
                        {
                            bsData.Add(dtoKhachHang);
                        }

                        if (chungminhnhandan != null && dem < 3)
                        {
                            frmDat_PHONG frm = new frmDat_PHONG();
                            frm.SOCMND = Txt_CMND.Text;
                            this.Close();
                        }
                    }
                }
                else
                {
                    dtoKhachHang = new KhachHangDTO();
                    dtoKhachHang.CMND = Txt_CMND.Text.ToString();
                    dtoKhachHang.HOTENKH = Txt_HoTen.Text.ToString();
                    if (Ra_Nam.Checked == true)
                        dtoKhachHang.GIOITINH = Ra_Nam.Text.ToString();
                    else if (Ra_Nu.Checked == true)
                        dtoKhachHang.GIOITINH = Ra_Nu.Text.ToString();
                    dtoKhachHang.NGAYSINH = Dt_NgaySinh.Value.ToShortDateString();
                    dtoKhachHang.DIACHI = Txt_DiaChi.Text.ToString();
                    dtoKhachHang.QUOCTICH = Txt_QuocTich.Text.ToString();
                    dtoKhachHang.SDTKH = Txt_Sodt.Text.ToString();

                    if (!busKhachHang.CapNhatThongTin(dtoKhachHang))
                    {
                        MessageBox.Show("Thất bại! Xin hãy kiểm tra lại");
                        return;
                    }  
                    ArrayList dsKhachHang = new ArrayList();
                    dsKhachHang.Add(dtoKhachHang);
                    bsData = new BindingSource(dsKhachHang, "");
                    this.Grid_KhachHang.DataSource = bsData;
                }
            }
            catch
            {
                MessageBox.Show("Xin hãy nhập đầy đủ thông tin.");
            }
            DieukienMo(false);
            LamMoi();
            Btn_Them.Focus();
        }

        private void bt_KhongLuu_Click(object sender, EventArgs e)
        {
            LamMoi();
            DieukienMo(false);
        }
       
        private void timer1_Tick_1(object sender, EventArgs e)
        {
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
                timer1.Enabled = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOpentextbox_Click(object sender, EventArgs e)
        {
            LamMoi();
            DieukienMo(false);
            Gru_TXS.Enabled = false;
            Txt_CMND.Enabled = true;
            Txt_CMND.Focus();
            Txt_HoTen.Enabled = true;
            Dt_NgaySinh.Enabled = true;


            //LamMoi();
            //DieukienMo(false);
            //Gru_TXS.Enabled = false;
            //Txt_CMND.ReadOnly = true;
            ////Txt_CMND.Focus();
            //Txt_CMND.Text = chungminhnhandan;
            //Txt_HoTen.Enabled = true;
            //Txt_HoTen.Focus();
            //Dt_NgaySinh.Enabled = true;
            //Txt_DiaChi.Enabled = true;
            //Txt_Sodt.Enabled = true;
            //Txt_QuocTich.Enabled = true;
        }

        private void panel7_Paint(object sender, PaintEventArgs e)
        { 

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Gru_TXS_Enter(object sender, EventArgs e)
        {

        }
    }
}
